//
//  PlSInputManagerManager.m
//  Injector
//
//  Created by ∞ on 14/11/07.
//  Copyright 2007 Emanuele Vulcano. All rights reserved.
//

#import "PlSInputManagerManager.h"
#import "PlSInputManagerPlugIn.h"

@implementation PlSInputManagerManager

- (id) init {
	if (self = [super init])
		plugIns = [NSMutableDictionary new];
	
	return self;
}

- (void) dealloc {
	[plugIns release];
	[super dealloc];
}

- (NSArray*) plugIns {
	return [plugIns allValues];
}

- (PlSPlugIn*) plugInWithIdentifier:(NSString*) ident {
	return [plugIns objectForKey:ident];
}

- (void) rescan {
	[plugIns removeAllObjects];
	NSFileManager* fm = [NSFileManager defaultManager];
	
	NSArray* libraries = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask | NSNetworkDomainMask | NSLocalDomainMask, YES);
	NSArray* localLibraries = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSLocalDomainMask, YES);
	NSArray* userLibraries = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
	BOOL loadedUserLibraries = NO;
	
	for (NSString* place in libraries) {
		BOOL isLocal = [localLibraries containsObject:place];
		
		NSString* inputManagers = [place stringByAppendingPathComponent:@"InputManagers"];
		BOOL isDir;
		
		if (![fm fileExistsAtPath:place isDirectory:&isDir] || !isDir)
			continue;
		
		if (!isLocal && !loadedUserLibraries && [userLibraries containsObject:place]) {
			// Inquisitor installer fix
			NSString* tigerPlace = [place stringByAppendingPathComponent:@"InputManagers-Tiger"];
			[self rescanAtPath:tigerPlace isLocal:NO];
			loadedUserLibraries = YES;
		}
		
		[self rescanAtPath:inputManagers isLocal:isLocal];
	}
}

- (void) rescanAtPath:(NSString*) inputManagers isLocal:(BOOL) isLocal {
	NSArray* items = [[NSFileManager defaultManager] directoryContentsAtPath:inputManagers];
	for (NSString* item in items) {
		if ([item hasPrefix:@"."]) continue;
		item = [inputManagers stringByAppendingPathComponent:item];
		
		PlSInputManagerPlugIn* pin = [[[PlSInputManagerPlugIn alloc] initWithPath:item manager:self isLocal:isLocal] autorelease];
		if (pin)
			[plugIns setObject:pin forKey:pin.identifier];
	}	
}

- (PlSInstallStatus) installPlugInAtPath:(NSString*) path andReturnError:(NSError**) error {
	NSFileManager* fm = [NSFileManager defaultManager];
	
	PlSInputManagerPlugIn* pin = [[PlSInputManagerPlugIn alloc] initWithPath:path manager:self isLocal:NO];
	if (!pin && [fm fileExistsAtPath:[[path stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Info"]]) {
		path = [path stringByDeletingLastPathComponent];
		pin = [[PlSInputManagerPlugIn alloc] initWithPath:[path stringByDeletingLastPathComponent] manager:self isLocal:NO];
	}
	
	if (!pin) return kPlSInstallIncompatible;
	[pin release]; // just to check.
	
	NSArray* arr = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
	if ([arr count] == 0) return kPlSInstallIncompatible; // TODO a real error.
	
	NSString* target = [[[arr objectAtIndex:0] stringByAppendingPathComponent:@"InputManagers"] stringByAppendingPathComponent:[path lastPathComponent]];
	if (![[NSFileManager defaultManager] copyItemAtPath:path toPath:target error:error])
		return kPlSInstallError;
	else
		return kPlSInstallDone;
}

@end
